//
//  SendFileKeyboard.m
//  JiaoX
//
//  Created by yinqy on 15/4/2.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "SendFileKeyboard.h"
#import "SendFileBtn.h"
#import <MobileCoreServices/MobileCoreServices.h>

@interface SendFileKeyboard()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@end
@implementation SendFileKeyboard

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor whiteColor]];
        
        SendFileBtn *pictureBtn = [self getBtnWithTitle:@"图片" image:@"ic_send_picture"];
        [self addSubview:pictureBtn];
        [pictureBtn addTarget:self action:@selector(pictureBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
        SendFileBtn *cameraBtn = [self getBtnWithTitle:@"拍照" image:@"ic_send_camera"];
        [cameraBtn setX:CGRectGetMaxX(pictureBtn.frame)];
        [self addSubview:cameraBtn];
        [cameraBtn addTarget:self action:@selector(cameraBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
        [self setSize:CGSizeMake(JXScreenW, CGRectGetMaxY(cameraBtn.frame)+SCALEX(12))];
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, JXScreenW, 0.5)];
        [view setBackgroundColor:JXColor(204, 204, 204)];
        [self addSubview:view];
        
    }
    return self;
}

-(SendFileBtn *)getBtnWithTitle:(NSString *)title image:(NSString *)image{
    SendFileBtn *btn = [[SendFileBtn alloc]initWithFrame:CGRectMake(0, SCALEX(10), JXScreenW/4, JXScreenW/4)];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setAdjustsImageWhenHighlighted:NO];
    [btn setImage:IMG(image) forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    return btn;
}

-(void)pictureBtnClick{
    [self openImgWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}

-(void)cameraBtnClick{
    [self openImgWithSourceType:UIImagePickerControllerSourceTypeCamera];
}

-(void)openImgWithSourceType:(UIImagePickerControllerSourceType)type{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.mediaTypes = @[(NSString *)kUTTypeImage];
    imagePickerController.sourceType = type;
    imagePickerController.delegate = self;
    UIViewController *controller = [self getCurrentVC];
    [controller presentViewController:imagePickerController animated:YES completion:nil];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    //获取图片裁剪的图
    UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
    //模态方式退出uiimagepickercontroller
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    NSData *data = UIImageJPEGRepresentation(image, 0.5);
    //回调
    if ([_delegate respondsToSelector:@selector(sendFileWithData:)]) {
        [_delegate sendFileWithData:data];
    }
}


//获取当前屏幕显示的viewcontroller
- (UIViewController *)getCurrentVC
{
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    
    return result;
}
@end
